﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Osiptel.HECO.BE;
using System.Data.OracleClient;
using System.Data;

namespace Osiptel.HECO.DL
{
    public class ComponentePrecioDL
    {
        public int mInsertar(ComponentePrecioBE componente)
        {

            try
            {
                Int32 id = 0;

                using (OracleCommand cmd = new OracleCommand())
                {

                    cmd.CommandText = "PKG_HECO_GLOBAL.SP_INSERTAR_COMPONENTEPRECIO";
                    cmd.CommandType = CommandType.StoredProcedure;
                    
                    OracleParameter prm1 = new OracleParameter("PPRECIOUNITARIO", OracleType.Number);
                    OracleParameter prm2 = new OracleParameter("PP02FUENTE", OracleType.Number);
                    OracleParameter prm3 = new OracleParameter("PNOMBREFUENTE", OracleType.VarChar);
                    OracleParameter prm4 = new OracleParameter("PIDCOMPONENTE", OracleType.Number);
                    OracleParameter prm5 = new OracleParameter("PUSUCRE", OracleType.VarChar);
                    OracleParameter prm6 = new OracleParameter("PIPREG", OracleType.VarChar);
                    OracleParameter prm7 = new OracleParameter("PIDCOMPPRECIO", OracleType.Number);

                    prm1.Direction = ParameterDirection.Input;
                    prm2.Direction = ParameterDirection.Input;
                    prm3.Direction = ParameterDirection.Input;
                    prm4.Direction = ParameterDirection.Input;
                    prm5.Direction = ParameterDirection.Input;
                    prm6.Direction = ParameterDirection.Input;
                    prm7.Direction = ParameterDirection.Output;

                    prm1.Value = componente.PrecioUnitario;
                    prm2.Value = componente.P02Fuente;
                    prm3.Value = componente.NombreFuente;
                    prm4.Value = componente.IdComponente;
                    prm5.Value = componente.UsuCre;
                    prm6.Value = componente.IpReg;
                    prm7.Value = componente.IdCompPrecio;

                    cmd.Parameters.Add(prm1);
                    cmd.Parameters.Add(prm2);
                    cmd.Parameters.Add(prm3);
                    cmd.Parameters.Add(prm4);
                    cmd.Parameters.Add(prm5);
                    cmd.Parameters.Add(prm6);
                    cmd.Parameters.Add(prm7);

                    using (DL_CONEXION NewConexion = new DL_CONEXION())
                    {
                        int ret = NewConexion.ejecutaSQL(cmd);
                        id = Convert.ToInt32(Convert.ToString(cmd.Parameters["PIDCOMPPRECIO"].Value));
                        return id;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<ComponentePrecioBE> ObtenerPrecioComponente(int idComp)
        {
            try
            {
                using (OracleCommand cmd = new OracleCommand())
                {
                    cmd.CommandText = "PKG_HECO_GLOBAL.SP_LISTAR_PRECIO_COMP";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("pIdComponente", OracleType.Number).Value = idComp;
                    cmd.Parameters.Add("oListado", OracleType.Cursor).Direction = ParameterDirection.Output;
                    using (DL_CONEXION NewConexion = new DL_CONEXION())
                    {
                        try
                        {
                            NewConexion.retOpen();
                            cmd.Connection = NewConexion.retConexion();
                            ComponentePrecioBE objCompPrecioBE;
                            List<ComponentePrecioBE> listaCompPrecio = new List<ComponentePrecioBE>();
                            using (OracleDataReader objDRParametroDet = cmd.ExecuteReader())
                            {
                                while (objDRParametroDet.Read())
                                {
                                    objCompPrecioBE = new ComponentePrecioBE();
                                    objCompPrecioBE.IdCompPrecio = Convert.ToInt32(objDRParametroDet["IDCOMPPRECIO"]);
                                    objCompPrecioBE.NombreFuente = Convert.ToString(objDRParametroDet["NOMBREFUENTE"]);
                                    objCompPrecioBE.P02Fuente = Convert.ToInt32(objDRParametroDet["P02FUENTE"]);
                                    objCompPrecioBE.P02FuenteDesc = Convert.ToString(objDRParametroDet["P02FUENTEDESC"]);
                                    objCompPrecioBE.PrecioUnitario = Convert.ToDecimal(objDRParametroDet["PRECIOUNITARIO"]);
                                    listaCompPrecio.Add(objCompPrecioBE);
                                }
                                objDRParametroDet.Close();
                            }
                            NewConexion.retClose();
                            return listaCompPrecio;
                        }
                        catch (Exception ex)
                        {
                            string error = ex.ToString();
                            List<ComponentePrecioBE> listaCompPrecio = new List<ComponentePrecioBE>();
                            return listaCompPrecio;
                        }
                        finally
                        {
                            if (NewConexion.conexion.State == ConnectionState.Open) NewConexion.retClose();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
